 /*
  *@(#)RemoteLogWriterImpl.java	28 Sept. 2000
  *
  * Copyright: MetaPlus Inc & Inquiry Management Systems Ltd.  2000
  *
  * This software is the proprietary information of MetaPlus Inc & 
  * Inquiry Management Systems Ltd.  
  * Use is subject to license terms.
  */
package com.ims.iaa;

import java.io.*; 
import java.sql.*;
import java.rmi.*;

/** 
  *
  * <p>This class <code>RemoteLogWriterImpl</code> provides the log write 
  * services for other classes. This class is act as an remote RMI services 
  * object which implements the <code>RemoteLogWriter</code> remote 
  * interface.</p>
  *
  * @see LogWriter
  *
  * @author Anthony Zhang
  *
  * @version 1.00, 25 Sept. 2000
  *
  */
public class RemoteLogWriterImpl 
	extends java.rmi.server.UnicastRemoteObject implements RemoteLogWriter {

	/**
	  * The PrintStream used to save log information
	  */
	private PrintStream ps = null;
	
	private int logLineCount = 0;
	
	public RemoteLogWriterImpl() throws RemoteException {
		//26 Oct 2000 Anthony Zhang	
		// check the flag to see if we need to create the log file
		if (!IaaDebug.LogWriteToScreen) createLogFile();	
	}

	public void println(String x) throws RemoteException {
		//Anthony Zhang modified Nov 16 2000 . Prevent create two log file at the same time.
		synchronized(this) {
			logLineCount++;
			if (logLineCount >= IaaDebug.MaxLineOfLog )  {
				closeLogFile();
				logLineCount = 0;
				createLogFile();
			}
		}
		String time = new Timestamp(System.currentTimeMillis()).toString();		
		ps.println(time + " : " + x);
	}

	public void printStackTrace(Throwable throwable) throws RemoteException {
		synchronized(this) {
			logLineCount++;
			if (logLineCount >= IaaDebug.MaxLineOfLog )  {
				closeLogFile();
				logLineCount = 0;
				createLogFile();
			}
		}
		throwable.printStackTrace(ps);
	}
 
 	private void createLogFile() {
		String fileName = getFileName();
		String tempName = fileName;

		//26 Oct 2000 Anthony Zhang		
		//check the log directory property and store log file 
		//under the specified directory
		if (!IaaDebug.LogDirectory.equals("")) {
		    //if(IaaDebug.Debug) System.out.println("Create log directory" + IaaDebug.LogDirectory);
		 	File path = new File(IaaDebug.LogDirectory);
 			try  {
 				if (!path.exists())	path.mkdirs();
				//if we can successfully create the directory than we can store info 
				//there. 
			 	fileName = IaaDebug.LogDirectory + "\\" + fileName;
		 	} catch (SecurityException e)  {
 				IaaUtil.log("Create dir error " + path + e);			
	 		}		
		}
		
		try  {
			ps = new PrintStream( new FileOutputStream(fileName));
		    if(IaaDebug.Debug) ps.println("Create log file successfully " + fileName);
		} catch (IOException e ) {
			System.out.println( "Create log file failed " + fileName + " :" + e );
			try  {
				ps = new PrintStream( new FileOutputStream(tempName));
			} catch (IOException ex ) {
				System.out.println( "Fatal error: create log file failed " + tempName + " :" + ex );
				ps = System.out;
			}
		}
 	}
	
	private static String getFileName() {
	    String sRet = "";
		try  {
		    sRet = java.net.InetAddress.getLocalHost().getHostName();
		} catch (Exception e)  {
			IaaUtil.log("Error get localhost name" + e);
			sRet = "Unknown";
		}
		String time = new Timestamp(System.currentTimeMillis()).toString();
		time = time.substring(0,19);
		time = time.replace(':', '-'); 
		time = time.replace(' ', '_'); 
		sRet += "_Log_" + time + ".log";
		
		return sRet;
	}
	
	private void closeLogFile()  {
		ps.println("Exceed max line of log file, close file :" + logLineCount);
		ps.close();
	}
}